<template>
  <div class=" wrapper ">
    <template v-for="(item, idx) in numStr" :key="idx">
      <PNum v-if="/\d/.test(item)" 
        :num="Number(item)" 
        :height="height" 
        :width="width" 
        :duration="duration - (duration /( numLength * 2) * ( numLength - idx - 1))"
        :revolution="revolution"></PNum>
      <div class="symbol" v-else>{{ item }}</div>
    </template>
  </div>
</template>
<script lang="ts">
export default{
  name: 'Pips'
}
</script>
<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import PNum from './PNum.vue'
/**
 * @param width 定义宽度默认50px
 */
const props = defineProps({
  /** 宽度 */
  width: {
    type: Number,
    default: 50
  },
  /** 高度 */
  height: {
    type: Number,
    default: 80
  },
  /** 数据 */
  num: {
    type: Number || String,
    default: 0
  },
  /** 动画时间 */
  duration: {
    type: Number,
    default: 1
  },
  /** 保留小数位数 */
  decimal: {
    type: Number,
    default: 0
  },
  /** 是否千分位 */
  thousands: {
    type: Boolean,
    default: false
  },
  numLength: {
    type: Number,
    default: 3
  },
  /** 数字转牌动画期间旋转圈数，0表示不满一圈 */
  revolution: {
    type: Number,
    default: 1
  }
})
const numStr = ref('0')
const format = (num: number | string) => {
  const numArr = num.toString().split('.')
  if (props.thousands) {
    numArr[0] = Number(numArr[0]).toLocaleString()
  }
  if (numArr[1]) {
    numArr[1] = numArr[1].slice(0, props.decimal)
  }
  numStr.value = props.decimal <= 0 ? numArr[0] : numArr.join('.')
}
onMounted(() => {
  
    let s1 = '1'
    for (let i = 0; i < props.numLength - 1; i++) {
      s1 += '0'
    }
    if (props.decimal > 0) {
      s1 = s1.replace(/(\d{2})$/, '.$1')
    }
    format(s1)
    if(props.num > 0){
      setTimeout(() => {
        format(props.num)
      }, 10);
    }
})

watch(() => props.num, v => {
  format(v)
})

</script>

<style lang="less" scoped>
.wrapper {
  display: flex;
  &>div {
    margin: 0 6px;
  }
  .symbol {
    display: flex;
    align-items: flex-end;
    color: #65DEFF;
    font-family: RanyMedium;
    font-size: 56px;
  }
}
</style>